package com.cusc.module_monitor_location.provider;

import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import androidx.core.view.ViewCompat;

import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cusc.module_monitor_location.R;
import com.cusc.module_monitor_location.adapter.NodeDepartmentAdapter;
import com.cusc.module_monitor_location.entry.NodeBean;
import com.tcgroup.phone.base_core.event.event_bus.BaseFragmentEvent;
import com.tcgroup.phone.base_core.utils.KLog;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class FirstProvider extends BaseNodeProvider {

    public FirstProvider() {
        addChildClickViewIds(R.id.iv_select);
        addChildClickViewIds(R.id.iv_get_leaf);
        addChildClickViewIds(R.id.tv_name);
    }

    @Override
    public int getItemViewType() {
        return 1;
    }

    @Override
    public int getLayoutId() {
        return R.layout.adapter_layout_department_item_01;
    }

    @Override
    public void convert(@NotNull BaseViewHolder helper, @NotNull BaseNode data) {
        NodeBean entity = (NodeBean) data;


        if (entity.getUiState() == NodeBean.STATE_CHECKED){//全选
            helper.setImageResource(R.id.iv_select,R.drawable.rectangle_select);
        }else if (entity.getUiState() == NodeBean.STATE_PART_CHECKED){ //部分选择
            helper.setImageResource(R.id.iv_select,R.drawable.rectangle_half_select);
        }else if (entity.getUiState() == NodeBean.STATE_UNCHECKED){ //未选
            helper.setImageResource(R.id.iv_select,R.drawable.rectangle_no_select);
        }

        helper.setText(R.id.tv_name, entity.getTitle());
        if (entity.getNodeType() == 1) {///*节点类型，1:机构 2:部门 3:车辆 4:人员*/
            helper.setImageResource(R.id.iv_choose_car, R.drawable.choose_sys);
        } else if (entity.getNodeType() == 2) {
            helper.setImageResource(R.id.iv_choose_car, R.drawable.choose_department);
        } else if (entity.getNodeType() == 3) {
            helper.setImageResource(R.id.iv_choose_car, R.drawable.choose_car);
        } else if (entity.getNodeType() == 4) {
            helper.setImageResource(R.id.iv_choose_car, R.drawable.choose_department);
        }

        if (entity.getLeaf() == 0) {
            helper.findView(R.id.iv_get_leaf).setVisibility(View.VISIBLE);
            helper.setImageResource(R.id.iv_get_leaf, R.drawable.core_back_left_gray);
        } else {
            helper.findView(R.id.iv_get_leaf).setVisibility(View.GONE);
        }
        setArrowSpin(helper, data, false);
    }

    @Override
    public void convert(@NotNull BaseViewHolder helper, @NotNull BaseNode data, @NotNull List<?> payloads) {
        for (Object payload : payloads) {
            if (payload instanceof Integer && (int) payload == NodeDepartmentAdapter.EXPAND_COLLAPSE_PAYLOAD) {
                // 增量刷新，使用动画变化箭头
                setArrowSpin(helper, data, true);
            }
        }
    }

    private void setArrowSpin(BaseViewHolder helper, BaseNode data, boolean isAnimate) {
        NodeBean entity = (NodeBean) data;
        ImageView imageView = helper.getView(R.id.iv_get_leaf);

        if (entity.isExpanded()) {
            if (isAnimate) {
                ViewCompat.animate(imageView).setDuration(200)
                        .setInterpolator(new DecelerateInterpolator())
                        .rotation(0f)
                        .start();
            } else {
                imageView.setRotation(0f);
            }
        } else {
            if (isAnimate) {
                ViewCompat.animate(imageView).setDuration(200)
                        .setInterpolator(new DecelerateInterpolator())
                        .rotation(90f)
                        .start();
            } else {
                imageView.setRotation(90f);
            }
        }
    }


    @Override
    public void onChildClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
        KLog.i("TAG","当前点击的位置"+position);
        if (view.getId() == R.id.iv_get_leaf) {
            // 这里使用payload进行增量刷新（避免整个item刷新导致的闪烁，不自然）
            NodeBean entity = (NodeBean) data;
            getAdapter().expandOrCollapse(position, true, true, NodeDepartmentAdapter.EXPAND_COLLAPSE_PAYLOAD);
            if (entity.getLeaf() == 0 && (entity.getChildNode() == null || entity.getChildNode().size() == 0)) {//有子节点
                BaseFragmentEvent<BaseNode> baseEvent = new BaseFragmentEvent<>(110);
                baseEvent.data = data;
                EventBus.getDefault().post(baseEvent);
            }
            return;
        }

        if (view.getId() == R.id.iv_select){

            return;
        }

        if (view.getId() == R.id.tv_name){

            return;
        }

    }

}
